package client

import (
	"context"
	"github.com/golang/protobuf/ptypes/wrappers"
	"github.com/micro/go-micro/v2/client"
	"github.com/micro/go-micro/v2/client/grpc"
	"kop/framework/wrapper"
	"kop/pb"
	"kop/pb/service"
)

var ShopService service.ShopService

func init() {

	var c = grpc.NewClient(client.Wrap(wrapper.ClientLogWrapper))
	_ = c.Init()

	//s.Init(test.Flags()...)

	ShopService = service.NewShopService("go.micro.srv.item", c)
}

func GetShop(ctx context.Context, req *wrappers.Int32Value) *pb.Shop {

	var shop, err = ShopService.GetShop(ctx, req)
	if err != nil {
		panic(err)
	}

	return shop
}

func GetGoodsList(roleID, shopID int32) []*pb.Goods {

	var goodsList, err = ShopService.GetGoodsList(context.Background(), &service.DefaultRequest{RoleID: roleID, Value: shopID})
	if err != nil {
		panic(err)
	}

	return goodsList.GoodsList
}

func GetGoods(roleID, goodsID int32) (*pb.Goods, error) {

	return ShopService.GetGoods(context.Background(), &service.DefaultRequest{RoleID: roleID, Value: goodsID})
}

func SaveGoods(roleID int32, goods *pb.Goods) {

	var resp, err = ShopService.SaveGoods(context.Background(), &service.SaveGoodsRequest{RoleID: roleID, Goods: goods})
	if err != nil {
		panic(err)
	}

	*goods = *resp
}

func Refresh(roleID int32, shop *pb.Shop) []*pb.Goods {

	var resp, err = ShopService.Refresh(context.Background(), &service.ShopRefreshRequest{RoleID: roleID, Shop: shop})
	if err != nil {
		panic(err)
	}

	*shop = *resp.Shop

	return resp.GoodsList
}
